/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package elaborate.tag_analysis.client.actions.reachability;

import elaborate.tag_analysis.client.graph.ClosureClusterBasedGraph;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author lendle
 */
public class ConnectedClustersGenerator {

    public void generate(File csv, File prefix, double threshold) throws IOException, NumberFormatException {
        ClosureClusterBasedGraph graph = new ClosureClusterBasedGraph();
        BufferedReader input = null;
        try {
            input = new BufferedReader(new InputStreamReader(new FileInputStream(csv), "utf-8"));
            String str = input.readLine();
            List<String> tagList = new ArrayList<String>();
            Map<Object, Integer> tag2IndexMap = new HashMap<Object, Integer>();
            //parse the header part to points
            String[] tags = str.split(",");
            int tagIndex = 0;
            for (String tag : tags) {
                if (tag.trim().length() > 0) {
                    graph.addPoint(tag);
                    tagList.add(tag);
                    tag2IndexMap.put(tag, tagIndex);
                    tagIndex++;
                }
            }
            /////////////////////////////////
            str = input.readLine();
            int index = 0;
            while (str != null) {
                //set up connected edges
                //System.out.println(graph.getClusters().size()+":"+dlg.getThreshold());
                String[] params = str.split(",");
                String src = params[0];
                for (int i = index + 1; i < tagList.size(); i++) {
                    String param = params[i + 1];

                    String target = tagList.get(i);
                    if (Double.valueOf(param).doubleValue() >= threshold) {
                        //connected
                        graph.setConnected(src, target);
                    }
                }
                index++;
                str = input.readLine();
            }
            //generate .net files
            int fileIndex = 0;
            //System.out.println(graph.getClusters().size());
            for (Cluster cluster : graph.getClusters()) {
                File file = new File(prefix.getAbsolutePath() + fileIndex + ".net");
                //System.out.println(file);
                PrintWriter output = null;
                try {
                    output = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), "utf-8"));
                    output.println("*Vertices " + cluster.getClusteredPoints().size());
                    int count = 1;
                    Map<Object, String> tag2NumericIdMap = new HashMap<Object, String>();
                    for (Object tag : cluster.getClusteredPoints()) {
                        output.println((count) + " \"" + tag + "\"");
                        tag2NumericIdMap.put(tag, "" + count);
                        count++;
                    }
                    output.println("*edges");
                    //undirected graph, so look for edges to tags at increasing index only
                    for (Object tag : cluster.getClusteredPoints()) {
                        String src = (String) tag;
                        int _tagIndex = tag2IndexMap.get(src);
                        for (int i = _tagIndex + 1; i < tagList.size(); i++) {
                            String target = tagList.get(i);
                            if (graph.isDirectlyConnected(src, target)) {
                                output.println(tag2NumericIdMap.get(src) + " " + tag2NumericIdMap.get(target));
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (output != null) {
                        output.close();
                    }
                }
                fileIndex++;
            }
        } finally {
            if (input != null) {
                input.close();
            }
        }
    }
}
